<!--
 * @Author: jswang
 * @Date: 2021-07-29 10:30:16
 * @LastEditTime: 2021-08-02 13:49:19
-->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容 头部 -->
      <el-card class="tree-card">
        <tree-tools  :tree-node="company" :is-root="true" @addDepts="addDepts">
        </tree-tools>
        <!-- 放置一个el-tree 组件 -->
        <el-tree :data="departs" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true" >
          <!-- 放置结构内容 -->
          <!-- {data}  是解构出来的,  slot-scope= "obj" 是一个对象   里面有两个属性 node  data -->
          <!-- delDepts 该事件为子组件中 通过 $emit('delDepts') 创建的自定义事件,在父组件中绑定该事件处理函数,达到操作子组件的效果 -->
        <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts">
        </tree-tools>
        </el-tree>
      </el-card>
    </div>
    <!-- 放置新增表单弹层组件 -->
    <!-- 子组件的需要的 props 必须传递过去 -->
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :treeNode="node" @addDepts="getDepartments"></add-dept>
  </div>
</template>

<script>
import {getDepartments} from '@/api/departments'
import {tranListToTreeData} from '@/utils'

import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
export default {
  components:{
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog:false, //默认不显示弹层
      node:null //记录当前点击的node节点
    }
  },
  created(){
    this.getDepartments()
  },
  methods:{
    async getDepartments(){
      // debugger
      const result = await getDepartments()
      this.company = {name: result.companyName,manager: '负责人', id:''}
      this.departs = tranListToTreeData(result.depts,'') //这里定义一个空串  因为 它是根 所有的子节点的数据pid 都是 ""
    },
    //监听 tree-tools中触发的点击添加子部门的事件
    //node 是当前点击的部门
    addDepts(node){
      this.showDialog = true
      this.node = node
    },
    //编辑
    editDepts(node){
      this.showDialog = true
      this.node = node
      // 应该在这里调用获取部门详情的方法
      this.$refs.addDept.getDepartmentDetail(node.id)
    },
    //关闭弹层
    test(value){
      this.showDialog = value
    },
    delgetDepartments(){

    },
    handleNodeClick(){
    }
  }
}


</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
